package com.lib.krismile.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

/*
 * @author ： zwh
 * @date : 2021/9/29
 * @description ：2021 . All rights reserved.
 */
public class AnimatorUtil {

    @SuppressLint("Recycle")
    public static void startAnimator(View view) {
        //创建平移动画 800就是移动的高度
        ObjectAnimator translationYAnimator = ObjectAnimator.ofFloat(view, "translationY", 0, -800);
        //创建x轴缩放动画
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(view, "scaleX", 0f, 1f, 1.5f, 0.85f);
        //创建Y轴缩放动画
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(view, "scaleY", 0f, 1f, 1.5f, 0.85f);
        //渐变动画
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f, 0.9f, 1f, 0f);
        //创建动画集合
        AnimatorSet animatorSet = new AnimatorSet();
        //添加需要播放的动画
        animatorSet.playTogether(translationYAnimator, scaleXAnimator, scaleYAnimator, alphaAnimator);
        //设置动画播放的时间
        animatorSet.setDuration(8000);
        //播放动画
        animatorSet.start();
        //动画监听
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (view != null && view.getParent() != null) {
                    ((ViewGroup) view.getParent()).removeView(view);
                }
            }
        });
    }
    public static void removeAnimator(View view){
        if (view != null && view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeAllViews();
        }
    }


}
